<template>
  <div class="content">
    <h1>应聘管理</h1>
    <div class="consultation-block">
      <div class="table" v-loading="tableLoading">
        <el-table size="mini" :data="tableDataFilter" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="jobName" label="职位名称"> </el-table-column>
          <el-table-column prop="type" label="职位类型"> </el-table-column>
          <el-table-column prop="salary" label="薪资"> </el-table-column>
          <el-table-column prop="jobLocation" label="职位地点"> </el-table-column>
          <el-table-column prop="xueli" label="学历"> </el-table-column>
          <el-table-column prop="tags" label="标签" :formatter="formatTags"> </el-table-column>
          <el-table-column prop="experience" label="经验"> </el-table-column>
          <el-table-column prop="toudiIdsLen" label="投递人数"> </el-table-column>
          <el-table-column prop="other" label="其他信息"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="warning"
                size="mini"
                @click="check(scope.row)"
                >审核</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="pageNum"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="投递人名单"
      :visible.sync="isVisible"
      width="50%"
      :before-close="handleClose"
    >
        <el-table size="mini" :data="userData" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="username" label="姓名"> </el-table-column>
          <el-table-column prop="xueli" label="学历"> </el-table-column>
          <el-table-column prop="experience" label="工作经验"> </el-table-column>
          <el-table-column prop="subject" label="专业"> </el-table-column>
          <el-table-column prop="rs" label="审核结果" :formatter="formatRs"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="detail(scope.row)"
                >详细资料</el-button
              >
              <el-button
                type="warning"
                size="mini"
                @click="doSH(scope.row, 'true')"
                >通过</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="doSH(scope.row, 'false')"
                >驳回</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="详细资料"
      :visible.sync="detailModal"
      width="50%"
      :before-close="handleClose2"
    >
      
        <el-divider content-position="left">
          <h4>基础信息</h4>
        </el-divider>
        <el-form
          ref="form"
          label-width="80px"
          label-position="left"
          :model="userDetail"
          style="margin: auto"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              size="mini"
              disabled
              v-model="userDetail.username"
              auto-complete="off"
              placeholder="未填写"
            ></el-input>
          </el-form-item>
          <el-form-item label="头像" prop="avator">
            <div style="display: flex; align-item: center; gap: 20px">
              <div class="img-blocks">
                <div style="position: relative">
                  <img :src="userDetail.avator" style="width: 80px" />
                </div>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="学历" prop="xueli">
            <el-input
              size="mini"
              disabled
              v-model="userDetail.xueli"
              auto-complete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="工作经验" prop="experience">
            <el-input
              size="mini"
              disabled
              v-model="userDetail.experience"
              auto-complete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="专业" prop="subject">
            <el-input
              size="mini"
              disabled
              v-model="userDetail.subject"
              auto-complete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="telephone">
            <el-input
              size="mini"
              disabled
              v-model="userDetail.telephone"
              auto-complete="off"
              placeholder="未填写"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-divider content-position="left">
          <h4>个人优势</h4>
        </el-divider>
        <el-input
          type="textarea"
          style="margin-bottom: 20px;"
          :autosize="{ minRows: 5, maxRows: 10 }"
          placeholder="未填写"
          v-model="userDetail.advantage"
        >
        </el-input>
        <el-divider content-position="left">
          <h4>期望职位</h4>
        </el-divider>
        <div class="flex-center" v-if="qiwang" style="margin-bottom: 40px;">
          <el-input
            size="mini"
            style="width: 300px"
            v-model="qiwang.job"
            placeholder="未填写"
          ></el-input>
          <el-input
            size="mini"
            style="width: 300px"
            v-model="qiwang.salary"
            placeholder="未填写"
          ></el-input>
          <el-input
            size="mini"
            style="width: 300px"
            v-model="qiwang.location"
            placeholder="未填写"
          ></el-input>
        </div>
        <el-divider content-position="left">
          <h4>教育经历</h4>
        </el-divider>
        <div
          class="flex-center"
          style="margin-bottom: 10px;margin-bottom: 40px;"
          v-for="(item, index) in eduDetail"
          :key="index"
        >
          时间：
          <el-input
            size="mini"
            style="width: 300px"
            v-model="item.time"
            placeholder="未填写"
          ></el-input>
          学校名称：
          <el-input
            size="mini"
            style="width: 300px"
            v-model="item.name"
            placeholder="未填写"
          ></el-input>
        </div>
        <el-divider content-position="left">
          <h4>工作经历</h4>
        </el-divider>
        <div
          class="flex-center"
          style="margin-bottom: 10px;"
          v-for="(e, index) in experienceDetail"
          :key="index"
        >
          时间：
          <el-input
            size="mini"
            style="width: 300px"
            v-model="e.time"
            placeholder="未填写"
          ></el-input>
          公司名称：
          <el-input
            size="mini"
            style="width: 300px"
            v-model="e.name"
            placeholder="未填写"
          ></el-input>
        </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserInfo,updateUser,getXuelis,getJob, deleteJob,getJobByUser,updateJob,getJobSalary,getJobTags,getJobExperience,getJobLocation,getJobTypes } from "../service.js";
export default {
  name: "publish",
  beforeCreate() {
    getJobSalary().then(res => {
      this.jobSalarys = res.code === 200 ? res.data : []
    })
    getJobTags().then(res => {
      this.jobTags = res.code === 200 ? res.data : []
    })
    getJobExperience().then(res => {
      this.jobExperiences = res.code === 200 ? res.data : []
    })
    getJobLocation().then(res => {
      this.jobLocations = res.code === 200 ? res.data : []
    })
    getJobTypes().then(res => {
      this.jobTypes = res.code === 200 ? res.data : []
    })
    getXuelis().then(res => {
      this.xuelis = res.code === 200 ? res.data : []
    })
  },
  mounted() {
    // 查询个人资料
    if (localStorage.getItem("meiyaWebInfo")) {
      this.userInfo = JSON.parse(localStorage.getItem("meiyaWebInfo"))
      getUserInfo(this.userInfo._id).then(res => {
        this.userInfo = res.data
        this.refreshTable();
      })
    }
  },
  data() {
    return {
      userInfo: {},
      tableData: [],
      tableDataFilter: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      isVisible: false,
      dialogTitle: "",
      form: {
        jobName: "",
        jobDesc: "",
        tags: [],
        salary: '',
        experience: '',
        jobLocation: '',
        companyCard: '',
        xueli: '',
      },
      jobSalarys: [],
      jobTags: [],
      jobExperiences: [],
      jobLocations: [],
      jobTypes: [],
      xuelis: [],
      userData: [],
      detailModal: false,
      userDetail: {},
      qiwang: [],
      eduDetail: [],
      experienceDetail: []
    };
  },
  methods: {
    formatRs(row) {
      let myTdIds = row.myTdIds
      if (myTdIds.find(v => v._id === this.currentRowOut._id)) {
        if (myTdIds.find(v => v._id === this.currentRowOut._id).examine === 'true') {
          return '已通过'
        } else {
          return '未通过'
        }
      } else {
        return '未审核'
      }
    },
    naviData() {
      this.tableDataFilter = this.tableData.slice(
        (this.pageNum - 1) * this.pageSize,
        this.pageSize * this.pageNum
      );
    },
    handleAvatarSuccess(info) {
      this.form.pics.push(info.url);
    },
    addUser() {
      this.isVisible = true;
      this.form = {
        username: "",
        pass: "",
        checkPass: "",
        title: "",
        column: "",
      };
      this.dialogTitle = "新建用户";
    },
    handleClose() {
      this.isVisible = false;
    },
    handleClose2() {
      this.detailModal = false;
    },
    handleCurrentChange(index) {
      this.pageNum = index
      this.naviData()
    },
    refreshTable() {
      getJobByUser({
        publisherId: this.userInfo._id
      }).then(async (res) => {
        this.tableData = res.code === 200 ? res.data.filter(v => v.examine === 'true') : [];
        for (let item of this.tableData) {
          item.tags = JSON.parse(item.tags)
          if (item.toudiIds) {
            item.toudiIds = JSON.parse(item.toudiIds)
            item.toudiIdsLen = item.toudiIds.length
          }
        }
        this.total = this.tableData.length;
        this.naviData()
      });
    },
    formatTags(row) {
      if (row.tags) {
        return row.tags.join(',')
      }
    },
    handleAvatarSuccess(rs) {
      this.form.companyCard = rs.url; 
    },
    async check(row) {
      this.currentRowOut = row
      if (!row.toudiIds) {
        this.$message.error('无人投递此岗位')
        return
      }
      let toudiIds = row.toudiIds
      let toudiUser = []
      for (let id of toudiIds) {
        let user = await getUserInfo(id)
        user.data.myTdIds = JSON.parse(user.data.myTdIds)
        toudiUser.push(user.data)
      }
      this.userData = toudiUser
      this.isVisible = true
    },
    doSH(row, examine) {
      let myTdIds = row.myTdIds
      
      if (myTdIds.find(v => v._id === this.currentRowOut._id)) {
        let index = myTdIds.findIndex(v => v._id === this.currentRowOut._id)
        myTdIds[index].examine = examine
      }
      // 更新用户
      updateUser(row._id, {
        myTdIds: JSON.stringify(myTdIds)
      }).then(res => {
        this.isVisible = false;
        this.$message.success('操作成功')
      })
    },
    detail(row) {
      this.userDetail = row
      this.detailModal = true
      this.qiwang = JSON.parse(row.qiwang)
      this.eduDetail = JSON.parse(row.eduDetail)
      this.experienceDetail = JSON.parse(row.experienceDetail)
    }
  },
};
</script>

<style lang="scss" scoped>
h1 {
  text-align: center;
  margin: 30px auto;
}
.content {
  width: 1200px;
  margin: 0 auto;
  min-height: 80vh;
}
.pagination {
  text-align: center;
  margin-top: 30px;
}
</style>
